

#import "DCMoveVolu.h"

@interface DCMoveVolu()

@property (strong, nonatomic)UISlider *volumeSlider;

@property (strong, nonatomic) IBOutlet UIImageView *icon;


@property (nonatomic , assign) NSInteger type;//用来标识视图类型1.声音  2.亮度
@end

@implementation DCMoveVolu

- (instancetype)initWithType:(NSInteger)type
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"DCMoveVolu" owner:self options:nil] lastObject];
    if (self)
    {
        self.type = type;

        self.volumeSlider = [[UISlider alloc] init];
        self.volumeSlider.width = self.height - 30;
        self.volumeSlider.height = 20;
        self.volumeSlider.left = (self.width - self.volumeSlider.width) / 2;
        self.volumeSlider.top = self.height / 2 - 20;
        self.volumeSlider.transform = CGAffineTransformMakeRotation(-M_PI/2);//旋转
        
        self.volumeSlider.minimumValue = 0;
        self.volumeSlider.maximumValue = 1.0;
        self.volumeSlider.value = [MPMusicPlayerController applicationMusicPlayer].volume;//音量
        [self.volumeSlider setMinimumTrackTintColor:RGBCOLOR(57, 144, 206, 1)];
        [self.volumeSlider setMaximumTrackTintColor:RGBCOLOR(131, 131, 131, 131)];
        [self.volumeSlider setThumbImage:[UIImage imageNamed:@"play_4"]
                                forState:UIControlStateNormal];//滑块图片
        
        [self.volumeSlider addTarget:self action:@selector(volumeSliderMoved:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:self.volumeSlider];
        
        if (type == 1)
        {
            self.icon.image = [UIImage imageNamed:@"play_2"];
            self.volumeSlider.value = [MPMusicPlayerController applicationMusicPlayer].volume;
        }
        else
        {
            self.icon.image = [UIImage imageNamed:@"play_3"];
            self.volumeSlider.value = [UIScreen mainScreen].brightness;//亮度
        }
    }
    return self;
}

//音量调节
- (void)volumeSliderMoved:(UISlider *)slider
{
    if (self.type == 1)
    {
        [MPMusicPlayerController applicationMusicPlayer].volume = slider.value;
    }
    else
    {
        [UIScreen mainScreen].brightness = slider.value;
    }
}

- (void)updateValue
{
    if (self.type == 1)
    {
        self.volumeSlider.value = [MPMusicPlayerController applicationMusicPlayer].volume;
    }
    else
    {
        self.volumeSlider.value = [UIScreen mainScreen].brightness;
    }
}

- (void)updateFrame
{
    
}

@end
